package com.crashcollection.lib;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class CrashInfo {
	private int mAndroidSdk;             // android sdk
	private String mAndroidRelease;      // android release
	private String mLanguage;            // 语言
	private int mAppVersion;             // 版本
	private String mNetworkType;         // 网络状态
	private String mCrashType;           // 异常类型
	private String mCrashInfo;           // 异常内容
	private String mCrashStackMd5;       // 异常stack Md5
	private long mCrashTime;             // 异常事件

	public void setAndroidSdk(int sdk) {
		mAndroidSdk = sdk;
	}

	public int getAndroidSdk() {
		return mAndroidSdk;
	}

	public void setAndroidRelease(String release) {
		mAndroidRelease = release;
	}

	public String getAndroidRelease() {
		return mAndroidRelease;
	}

	public void setLanguage(String language) {
		mLanguage = language;
	}

	public String getLanguage() {
		return mLanguage;
	}

	public void setAppVersion(int version) {
		mAppVersion = version;
	}

	public int getAppVersion() {
		return mAppVersion;
	}

	public void setNetworkType(String networkType) {
		mNetworkType = networkType;
	}

	public String getNetworkType() {
		return mNetworkType;
	}

	public void setCrashType(String crashType) {
		mCrashType = crashType;
	}

	public String getCrashType() {
		return mCrashType;
	}

	public void setCrashInfo(String crashInfo) {
		mCrashInfo = crashInfo;
	}

	public String getCrashInfo() {
		return mCrashInfo;
	}

	public void setCrashStackMd5(String crashStackMd5) {
		mCrashStackMd5 = crashStackMd5;
	}

	public void setCrashStackMd5(int appVersion, String crashStack) {
		String s = String.format(Locale.getDefault(), "%d:%s", appVersion,crashStack);
		mCrashStackMd5 = Md5Digest(s, true);
	}

	public String getCrashStackMd5() {
		return mCrashStackMd5;
	}

	public void setCrashTime(long createTime) {
		mCrashTime = createTime;
	}

	public long getCrashTime() {
		return mCrashTime;
	}

	private String Md5Digest(String aSrc, boolean aUpperCase) {
		String sResult = "";
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(aSrc.getBytes());
			byte b[] = md5.digest();
			StringBuffer buf = new StringBuffer("");
			// 转16进制
			for (int offset = 0; offset < b.length; offset++) {
				int i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			sResult = buf.toString();
			if (aUpperCase) {
				sResult = sResult.toUpperCase(Locale.getDefault());
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return sResult;
	}

}
